﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace DAL
{
    public class StudentRepository:IStudentRepository
    {
        //public static string connectionString = ConfigurationManager.ConnectionStrings["projectbConnection"].ToString();
        private SqlConnection conn;
        public StudentRepository(SqlConnection sqlconnnection)
        {
            this.conn = sqlconnnection;
        }
        /// <summary>
        /// select student for studentlist
        /// </summary>
        /// <param name="courseId"></param>
        /// <returns></returns>
        public List<Model.Student> selectStudent_Left(int courseId)
        {
            DataSet ds = new DataSet();
            List<Model.Student> student = new List<Model.Student>();
            
                try
                {
                    conn.Open();
                    SqlCommand comm = new SqlCommand("select StudentName, StudentId from Student where StudentId not in (select StudentID from CourseDetails where CourseId=" + courseId + ")", conn);
                    SqlDataAdapter da = new SqlDataAdapter(comm);
                    da.Fill(ds, "Student");
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        Model.Student s = new Model.Student
                        {
                            StudentName = row[0].ToString(),
                            StudentId = Convert.ToInt32(row[1])
                        };
                        student.Add(s);
                    }

                    return student;
                }
                catch (SqlException e)
                {
                    throw new Exception(e.Message);
                }
            }
        
        /// <summary>
        /// select student for selected list
        /// </summary>
        /// <param name="courseId"></param>
        /// <returns></returns>
        public List<Model.Student> selectStudent_Right(int courseId)
        {
            DataSet ds = new DataSet();
            List<Model.Student> student = new List<Model.Student>();
            
                try
                {
                    conn.Open();
                    SqlCommand comm = new SqlCommand("select StudentName, StudentId from Student where StudentId in (select StudentID from CourseDetails where CourseId=" + courseId + ")", conn);
                    SqlDataAdapter da = new SqlDataAdapter(comm);
                    da.Fill(ds);
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        Model.Student s = new Model.Student
                        {
                            StudentName = row[0].ToString(),
                            StudentId = Convert.ToInt32(row[1])
                        };
                        student.Add(s);
                    }
                    return student;
                }
                catch (SqlException e)
                {
                    throw new Exception(e.Message);
                }
            }
        }
    }

